package com.seeyon.apps.kk.form.workflowevent;

import java.sql.SQLException;
import java.time.LocalDateTime;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.template.CtpTemplate;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;
import com.seeyon.kk.utils.CAP4FormKitUtils;

/**
 * 处理事件清除表单抄送控件值，解决最后一个节点添加抄送人之后抄送多次
 * 
 * @author Shinelon
 *
 */
public class KkCcExtendPlug extends KkBaseExtendPlug {

	private final Log log = LogFactory.getLog(KkCcExtendPlug.class);
	private CAP4FormManager formManager;

	public CAP4FormManager getFormManager() {
		return formManager;
	}

	public void setFormManager(CAP4FormManager formManager) {
		this.formManager = formManager;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return "kkRemoveCc";
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "通用清除抄送人";
	}

	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}

	/**
	 *  处理前事件
	 */
	public WorkflowEventResult onBeforeFinishWorkitem(WorkflowEventData data) {
		try {
			log.info("处理=======start: " + LocalDateTime.now());
			FormBean formBean = (FormBean) data.getBusinessData().get("formBean");
			FormDataMasterBean formDataMasterBean = (FormDataMasterBean) data.getBusinessData().get("formDataBean");

			CAP4FormKitUtils.setCellValue(formDataMasterBean, "抄送-填写", null);
			this.formManager.saveOrUpdateFormData(formDataMasterBean, formBean.getId(), true);
			log.info("处理=======end: " + LocalDateTime.now());
		} catch (BusinessException e) {
			log.info("处理前事件：error :", e);
		} catch (SQLException e) {
			log.info("处理前事件：error SQL:", e);
		}

		return null;
	}
}
